<template>
  <div class="">
    <el-dialog
      :title="addFormData.id ? '编辑用户' : '新增用户'"
      :visible="showAddDialog"
      @close="btnCancels"
    >
      <el-form
        label-width="140px"
        :model="addFormData"
        style="padding-right:180px"
        :rules="rules"
        ref="addFormData"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addFormData.username" />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addFormData.email" />
        </el-form-item>

        <el-form-item v-if="!addFormData.id" label="密码" prop="password">
          <el-input v-model="addFormData.password" />
        </el-form-item>

        <el-form-item label="角色">
          <el-input v-model="addFormData.role" />
        </el-form-item>

        <el-form-item label="权限组名称">
          <!-- <el-input v-model="addForm.permission_group_title" /> -->
          <el-select
            v-model="addFormData.permission_group_id"
            placeholder="请选择"
            style="width:49%;margin-right:2%"
          >
            <el-option value="超级管理员组"></el-option>
            <el-option value="编辑组"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="联系电话">
          <el-input v-model="addFormData.phone" />
        </el-form-item>

        <el-form-item label="介绍">
          <el-input
            type="textarea"
            placeholder="Places input"
            v-model="addFormData.introduction"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button @click="btnCancels">取 消</el-button>
          <el-button type="primary" @click="btnOKs">确 定</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addManagementList, editManagementList } from '@/api/management'
export default {
  name: 'ok',
  components: {},
  props: {
    showAddDialog: {
      type: Boolean,
      default: false
    },
    addFormData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    btnOKs() {
      try {
        this.$refs.addFormData.validate(async isOK => {
          if (isOK) {
            if (this.addFormData.id) {
              // 编辑
              await editManagementList(this.addFormData)
            } else {
              // 新增
              await addManagementList(this.addFormData)
            }
          }
        })
        this.$emit('btnOKs')
        // this.$parent.getManagementList()
        this.$emit('update:showAddDialog', false)
        this.$message.success('操作成功')
      } catch (error) {
        this.$message.error('操作失败')
      }
      // console.log(this.addFormData)
    },
    btnCancels() {
      this.$refs.addFormData.resetFields()
      this.$emit('btnCancels')
      this.$emit('update:showAddDialog', false)
    }
  }
}
</script>
<style lang="less" scoped></style>
